/* 
 * This file is part of the Coffee Survey Framework - http://coffee.codeplex.com
 */
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace Coffee.SurveyControls.SurveyQuestions
{

    /// <summary>
    /// Summary description for SQYesNo
    /// </summary>
    public class SQYesNo : SurveyQuestionRow
    {

        public SQYesNo(SurveyControl parent, XmlNode configuration)
            : base(parent, configuration)
        {
        }

        public bool? Value
        {
            get { return ((TriStateControl)currentAnswerControl).Value; }
            set { ((TriStateControl)currentAnswerControl).Value = value; }
        }

        public override void LoadData()
        {
            Label tPrev = (Label)previousAnswerControl;
            TriStateControl tri = (TriStateControl)currentAnswerControl;

            if (Parent.Mapper != null)
            {
                bool? prev = (bool?)Parent.Mapper.Previous.GetProperty(ctlId);
                if(prev.HasValue)
                    tPrev.Text = prev.Value ? "Yes": "No";
                tri.Value = (bool?)Parent.Mapper.Current.GetProperty(ctlId);

                Parent.Parent.Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "LoadTriStateControl" + ctlId, "<script>ShowDependents('" + ctlId + "', '" + tri.Value + "');</script>");
            }
        }

        public override void SaveData()
        {
            TriStateControl tri = ((TriStateControl)currentAnswerControl);

            if (Parent.Mapper != null)
            {
                Parent.Mapper.Current.SetProperty(ctlId, tri.Value);
            }
        }

        protected override Control CreateCurrentDataControl()
        {
            TriStateControl tCurr = new TriStateControl();
            tCurr.GroupName = config.Id;
            tCurr.CssClass = "surveytable-text surveytable-radiobutton";
            return tCurr;
        }

        protected override Control CreatePreviousDataControl()
        {
            Label tPrev = new Label();
            tPrev.CssClass = "surveytable-text surveytable-radiobutton";
            return tPrev;
        }

        protected override void Initialize()
        {
            XmlNode validation = rootNode.SelectSingleNode("validation");

            //client side validation...
            //if (validation != null)
            //{
            //    // make sure to escape message!!!
            //    StringBuilder script = new StringBuilder();
            //    string message = validation.SelectSingleNode("message/text()").Value;

            //    script.Append("<script>function CheckTriStateGroup" + group + "Value(){var result = false;");
            //    foreach (XmlNode condition in validation.SelectNodes("condition"))
            //    {
            //        string condValue = condition.SelectSingleNode("text()").Value;
            //        if (condValue == "Yes")
            //        {
            //            script.Append("result = result || document.getElementById('" + rbYes.ClientID + "').checked;");
            //        }
            //        else if (condValue == "No")
            //        {
            //            script.Append("result = result || document.getElementById('" + rbNo.ClientID + "').checked;");
            //        }
            //        else if (condValue == "{empty}")
            //        {
            //            script.Append("result = result || (!document.getElementById('" + rbYes.ClientID + "').checked && !document.getElementById('" + rbNo.ClientID + "').checked);");
            //        }
            //    }
            //    script.Append("return result;}</script>");

            //    Page.RegisterStartupScript("SurveyQuestionTableCheckTriStateGroup" + group, script.ToString());
            //    Page.RegisterStartupScript("SurveyQuestionTableValidateTriStateGroup" + group, "<script>addValidateEvent(function(){if(CheckTriStateGroup" + group + "Value()){ return confirm('" + message + "');} return true; });</script>");

            //}
        }
    }
}